package com.syn;

/**
 * 同步代码块
 */
public class Demo1_Synchronized {
    public static void main(String[] args){
        final Printer p = new Printer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    p.print();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    p.print1();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    p.print2();
                }
            }
        }).start();
    }
}

class Printer{
    Demo d = new Demo();
    public void print(){
        synchronized (d){
            System.out.print("黑");
            System.out.print("马");
            System.out.print("程");
            System.out.print("序");
            System.out.print("员");
            System.out.print("\r\n");
        }
    }

    public void print1(){
        synchronized (d){
            System.out.print("传");
            System.out.print("智");
            System.out.print("播");
            System.out.print("客");
            System.out.print("\r\n");
        }
    }

    //非静态的同步方法的锁对象是神马?
    //答:非静态的同步方法的锁对象是this
    //静态的同步方法的锁对象是什么?
    //是该类的字节码对象
    public static synchronized void print2() {							//同步方法只需要在方法上加synchronized关键字即可
        System.out.print("xasdf");
        System.out.print("asdfs");
        System.out.print("asdf");
        System.out.print("asf");
        System.out.print("asfd");
        System.out.print("\r\n");
    }
}

class Demo{

}
